﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Stonecode.Solar.ParticleEngine.ParticleSystems;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Stonecode.Solar.ParticleEngine.Emitters
{
    public enum EmitterType
    {
        Circle,Rectangle,Point,Line
    };
    public abstract class Emitter
    {
        public string EmitterName { get; set; }

        /// <summary>
        /// Sets whether to create particles in the Update() method. Default is true
        /// </summary>
        public bool IsAlive = true;

        public bool AutoGenerate { get; set; }

        /// <summary>
        /// Sets whether this emitter is enabled. Default is true
        /// </summary>
        public bool Enabled = true;

        //public string EmitterName = "";
        public EmitterType EmitterType;
        protected ParticleSystem _particleSystem;
        protected float timeBetweenParticles;
        protected float timeLeftOver;

        public Vector2 Offset;
        public Vector3 Position;

        public ParticleSystem ParticleSystem { get { return _particleSystem; } }

        public Emitter(GraphicsDevice graphics, Effect pEffect, EmitterSettings psettings,
            Texture2D texture, float particlesPerSecond, Matrix view, Matrix proj, EmitterType type)
        {
            _particleSystem = new ParticleSystem(graphics, ref pEffect, psettings, texture, this);
            _particleSystem.SetCamera(view, proj);
            timeBetweenParticles = 1.0f / psettings.ParticlesPerSecond;
            EmitterType = type;
            AutoGenerate = true;
        }

        public virtual void Dispose()
        {
            ParticleSystem.Dispose();
            EmitterName = null;
        }

        public virtual void Update(float dt)
        {
            if (Enabled)
            {
                timeBetweenParticles = 1.0f / _particleSystem.Settings.ParticlesPerSecond;
                _particleSystem.Update(dt);
            }
        }

        public virtual void Trigger(float dt) { }
        public virtual void Trigger(int particleCount) { }
        public virtual void TriggerWithOffset(float dt, Vector2 offset) { }

        public virtual void Draw(GraphicsDevice graphics, float dt) 
        {
            if (Enabled)
                _particleSystem.Draw(dt, graphics);
        }

        public virtual void ClearParticles()
        {
            if (_particleSystem != null)
                _particleSystem.ClearParticles();
        }

    }
}
